package movie.controller;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Consumer;
import movie.entity.Order;
import movie.entity.OrderSeat;
import movie.service.OrderService;
import movie.service.SeatService;

@Controller
public class OrderInsertController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private SeatService seatService;
	@RequestMapping(value="/orderInsert",method=RequestMethod.GET)
	public String get(HttpServletRequest request) throws UnsupportedEncodingException, InterruptedException
	{
		//订单号
		//付款金额,@RequestParam("total_amount")float total_amount
		//@RequestParam("trade_no")String trade_no,@RequestParam("total_amount")String total_amount
		HttpSession session = request.getSession(false);
		//获取支付宝传来的信息：订单号+付款金额
		String trade_no=request.getParameter("trade_no");
		String total_amount=request.getParameter("total_amount");
		System.out.println(trade_no);
		System.out.println(total_amount);
		float allprice=Float.parseFloat(total_amount);
		//new一个order实体进行订单插入
		Order order=new Order();
		order.setAllPrice(allprice);
		order.setOrderId(trade_no);
		//从session里面获取排片号+用户
		String scheduleId=(String)session.getAttribute("scheduleId");
		String seats[]=(String[])session.getAttribute("seats");
		int Schedule_id=Integer.parseInt(scheduleId);
		//
		Consumer consumer = (Consumer)session.getAttribute("cur_user");
		int consumer_id=consumer.getUserId();//(int)session.getAttribute("consumer_id");
		order.setScheduleId(Schedule_id);
		order.setConsumerId(consumer_id);
		System.out.println(order.toString());
		orderService.insertOrder(order);
		//向orderSeat表插入座位
		OrderSeat orderSeat=new OrderSeat();
		for(String item:seats) {
			orderSeat.setOrderId(trade_no);
			int seatId=Integer.parseInt(item);
			System.out.println(seatId);
			System.out.println(trade_no);
			orderSeat.setSeatId(seatId);
			orderService.insertOrderSeat(orderSeat);
			seatService.setSeatState(seatId);
		}
		//返回支付成功界面
		return "redirect:/login";
		
	}
}
